package com.bw.car.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.car.entity.AddCarEntity
import com.bw.car.entity.CarPayEntity
import com.bw.car.entity.PayNewEntity
import com.bw.car.entity.PaymentEntity
import com.bw.car.entity.StopCarEntityItem
import com.bw.car.model.CarPayRepository
import com.bw.common.vm.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class CarPayViewModel @Inject constructor(private val repo:CarPayRepository):BaseViewModel () {
    //定义LiveData缓存数据
    val mCarPaySuccess = MutableLiveData<List<PaymentEntity>>()
    fun getCarPayData(){
        viewModelScope.launch(mDispatcher) {
            //调用仓库方法获取数据
            val res = repo.loadCarPayData()
            Log.d("--qqq", res.code.toString())
            if (res.code == 200){
                mCarPaySuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //添加车辆
    val mAddCarSuccess = MutableLiveData<AddCarEntity>()
    fun getAddCarData(carEntity: AddCarEntity){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadAddCarData(carEntity)
            if (res.code ==200){
                mAddCarSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //获取停车记录
    val mStopCarSuccess = MutableLiveData<List<StopCarEntityItem>>()
    fun getStopCarRecordData(){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadStopCarRecord()
            if (res.code == 200){
                mStopCarSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //立即支付
    val mPayNewSuccess = MutableLiveData<PayNewEntity>()
    fun getPaymentData(payNew: PayNewEntity){
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadPaymentData(payNew)
            if (res.code ==200){
                mPayNewSuccess.postValue(res.data)
            }else{
                mError.postValue("请求失败${res.message}")
            }
        }
    }

}